package cc.lj.internal.payment.domain.pay.service.impl;

import cc.lj.internal.core.model.PageBean;
import cc.lj.internal.core.model.PageParam;
import cc.lj.internal.payment.domain.pay.repository.ITicketPayRecordRepository;
import cc.lj.internal.payment.domain.pay.service.ITicketPayRecordDomainService;
import cc.lj.internal.payment.domain.pay.vo.TicketPayRecordDO;
import cc.lj.internal.payment.domain.pay.vo.convert.TicketPayRecordPOConvertDO;
import cc.lj.internal.payment.infrastructure.po.TicketPayRecordPO;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 合同收券表领域服务service实现类
 * @author liangxifeng
 * @date 2021-01-12
 */
@Service("ticketPayRecordDomainServiceImpl")
@Slf4j
public class TicketPayRecordDomainServiceImpl implements ITicketPayRecordDomainService {
    //电子券批次表仓储接口
    @Resource(name = "ticketPayRecordRepository")
    private ITicketPayRecordRepository repository;

    @Override
    public Integer insert(TicketPayRecordDO ticketPayRecordDO) {
        TicketPayRecordPO ticketPayRecordPO = TicketPayRecordPOConvertDO.INSTANCE.convert(ticketPayRecordDO);
        return repository.insert(ticketPayRecordPO);
    }

    /**
     * 分页查询
     * @param pageParam
     * @return
     */
    @Override
    public PageBean<TicketPayRecordDO> selectPage(PageParam<TicketPayRecordDO> pageParam) {
        PageInfo<TicketPayRecordPO> pageInfo = repository.selectPage(pageConvert(pageParam));

        List<TicketPayRecordDO> list = new ArrayList();
        if(pageInfo.getList().size() > 0)
        {
            for (TicketPayRecordPO TicketPayRecordPO : pageInfo.getList()) {
                TicketPayRecordDO activityDO = TicketPayRecordPOConvertDO.INSTANCE.convert(TicketPayRecordPO);
                list.add(activityDO);
            }
        }
        //list的po转换为do后再组装PageBean将其返回上一层
        PageBean pageBean = PageBean.createPageBean(pageInfo.getPageNum(),
                pageInfo.getPageSize(), pageInfo.getTotal(), list);
        return pageBean;
    }


    /**
     * 分页DO to PO
     * @param pageParam
     * @return
     */
    public PageParam<TicketPayRecordPO> pageConvert(PageParam<TicketPayRecordDO> pageParam) {
        PageParam<TicketPayRecordPO> pageParamR = new PageParam<TicketPayRecordPO>();
        pageParamR.setPageNum( pageParam.getPageNum() ) ;
        pageParamR.setPageSize( pageParam.getPageSize() );
        pageParamR.setOrderBy( pageParam.getOrderBy() );
        TicketPayRecordPO activityPO = TicketPayRecordPOConvertDO.INSTANCE.convert(pageParam.getParam());
        pageParamR.setParam(activityPO);
        return pageParamR;
    }


    /**
     * 根据id修改
     * @param ticketPayRecordDO
     * @return 影响行数
     */
    public Integer updateById(TicketPayRecordDO ticketPayRecordDO) {
        TicketPayRecordPO ticketPayRecordPO = TicketPayRecordPOConvertDO.INSTANCE.convert(ticketPayRecordDO);
        return repository.updateById(ticketPayRecordPO);
    }


    /**
     * 标准批量获取数据的方法
     * @param ticketPayRecordDO
     * @return list
     */
    public List<TicketPayRecordDO> getList(TicketPayRecordDO ticketPayRecordDO)
    {
        TicketPayRecordPO TicketPayRecordPO = TicketPayRecordPOConvertDO.INSTANCE.convert(ticketPayRecordDO);
        List<TicketPayRecordPO> list = repository.getList(TicketPayRecordPO);
        List<TicketPayRecordDO> newList = new ArrayList<>();
        for(TicketPayRecordPO item : list)
        {
            newList.add(TicketPayRecordPOConvertDO.INSTANCE.convert(item));
        }
        return newList;
    }

    @Override
    public BigDecimal getMartSharePriceSum(TicketPayRecordDO ticketPayRecordDO) {
        TicketPayRecordPO recordDO = TicketPayRecordPOConvertDO.INSTANCE.convert(ticketPayRecordDO);
        return repository.getMartSharePriceSum(recordDO);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,timeout=36000,rollbackFor=Exception.class)
    public void batchAdd(List<TicketPayRecordDO> listDO) {
        for (TicketPayRecordDO ticketPayRecordDO : listDO) {
            TicketPayRecordPO recordPO = TicketPayRecordPOConvertDO.INSTANCE.convert(ticketPayRecordDO);
            repository.insert(recordPO);
        }
    }
}
